﻿using Robot.Activities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MutilVersionApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            RunByDomainVersion(versionCB.Text);
        }
        public void RunByDomainVersion(string version)
        {
            var MathDomanV0 = AppDomain.CreateDomain($"MathDoman{version}");

            var mainFolder = "C:\\mypo\\MutilVersionApp\\MutilVersionApp\\bin\\Debug";
            var v0Path = System.IO.Path.Combine(mainFolder, $"Math{version}", "Robot.Activities.Math.dll");
            if (System.IO.File.Exists(v0Path))
            {
                var assembly = Assembly.LoadFile(v0Path);
                DomainRun(assembly);
            }
            AppDomain.Unload(MathDomanV0);
        }

        public void DomainRun(Assembly domainAssembly)
        {

            var addObj = (IAction)domainAssembly.CreateInstance("Robot.Activities.Math.AddAction");
            var subcObj = (IAction)domainAssembly.CreateInstance("Robot.Activities.Math.SubtractionAction");

            AddResult = RunNode(addObj);
            SubCribResult = RunNode(subcObj);
        }

        public int RunNode(IAction action)
        {
            var result = 0;
            var addProps = action.GetType().GetProperties();

            foreach (var prop in addProps)
            {
                if (prop.Name == "A")
                {
                    prop.SetValue(action, A);
                }
                if (prop.Name == "B")
                {
                    prop.SetValue(action, B);
                }
            }

            action.Run();

            foreach (var prop in addProps)
            {
                if (prop.Name == "Result")
                {
                    result = (int)prop.GetValue(action);
                }
            }
            return result;
        }


        public int A
        {
            get { return (int)GetValue(AProperty); }
            set { SetValue(AProperty, value); }
        }

        // Using a DependencyProperty as the backing store for A.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty AProperty =
            DependencyProperty.Register("A", typeof(int), typeof(MainWindow), new PropertyMetadata(0));



        public int B
        {
            get { return (int)GetValue(BProperty); }
            set { SetValue(BProperty, value); }
        }

        // Using a DependencyProperty as the backing store for B.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty BProperty =
            DependencyProperty.Register("B", typeof(int), typeof(MainWindow), new PropertyMetadata(0));




        public int AddResult
        {
            get { return (int)GetValue(AddResultProperty); }
            set { SetValue(AddResultProperty, value); }
        }

        // Using a DependencyProperty as the backing store for AddResult.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty AddResultProperty =
            DependencyProperty.Register("AddResult", typeof(int), typeof(MainWindow), new PropertyMetadata(0));



        public int SubCribResult
        {
            get { return (int)GetValue(SubCribResultProperty); }
            set { SetValue(SubCribResultProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SubCribResult.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SubCribResultProperty =
            DependencyProperty.Register("SubCribResult", typeof(int), typeof(MainWindow), new PropertyMetadata(0));

    }
}
